home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / COLLECT.PAK / PTARRYVW.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  4KB  |  191 lines

  1. // ptarryvw.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "collect.h"
  15. #include "colledoc.h"
  16. #include "ptarryvw.h"
  17. #include "resource.h"
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CPointArrayView
  26.  
  27. IMPLEMENT_DYNCREATE(CPointArrayView, CFormView)
  28.  
  29. CPointArrayView::CPointArrayView()
  30.     : CFormView(CPointArrayView::IDD)
  31. {
  32.     //{{AFX_DATA_INIT(CPointArrayView)
  33.     m_x = 0;
  34.     m_y = 0;
  35.     //}}AFX_DATA_INIT
  36. }
  37.  
  38. CPointArrayView::~CPointArrayView()
  39. {
  40. }
  41.  
  42. void CPointArrayView::OnInitialUpdate()
  43. {
  44.     CFormView::OnInitialUpdate();
  45.  
  46.     // Copy all of the points from the document's CArray of CPoints
  47.     // to the listbox.
  48.     m_ctlList.ResetContent();
  49.     CArray<CPoint,CPoint>&ptArray = GetDocument()->m_ptArray;
  50.     for (int nIndex = 0; nIndex < ptArray.GetSize(); nIndex++)
  51.     {
  52.         CPoint pt = ptArray[nIndex];
  53.         AddPointToListBox(pt);
  54.     }
  55. }
  56.  
  57.  
  58. void CPointArrayView::DoDataExchange(CDataExchange* pDX)
  59. {
  60.     CFormView::DoDataExchange(pDX);
  61.     //{{AFX_DATA_MAP(CPointArrayView)
  62.     DDX_Control(pDX, IDC_LIST, m_ctlList);
  63.     DDX_Text(pDX, IDC_X, m_x);
  64.     DDX_Text(pDX, IDC_Y, m_y);
  65.     //}}AFX_DATA_MAP
  66. }
  67.  
  68.  
  69. BEGIN_MESSAGE_MAP(CPointArrayView, CFormView)
  70.     //{{AFX_MSG_MAP(CPointArrayView)
  71.     ON_BN_CLICKED(IDC_ADD, OnAdd)
  72.     ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
  73.     ON_BN_CLICKED(IDC_REMOVE, OnRemove)
  74.     ON_BN_CLICKED(IDC_REMOVE_ALL, OnRemoveAll)
  75.     ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
  76.     //}}AFX_MSG_MAP
  77. END_MESSAGE_MAP()
  78.  
  79.  
  80.  
  81. /////////////////////////////////////////////////////////////////////////////
  82. // CPointArrayView diagnostics
  83.  
  84. #ifdef _DEBUG
  85. void CPointArrayView::AssertValid() const
  86. {
  87.     CFormView::AssertValid();
  88. }
  89.  
  90. void CPointArrayView::Dump(CDumpContext& dc) const
  91. {
  92.     CFormView::Dump(dc);
  93. }
  94.  
  95. CCollectDoc* CPointArrayView::GetDocument() // non-debug version is inline
  96. {
  97.     return STATIC_DOWNCAST(CCollectDoc, m_pDocument);
  98. }
  99. #endif //_DEBUG
  100.  
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CPointArrayView internal implementation
  104.  
  105. void CPointArrayView::AddPointToListBox(CPoint pt, int nSel)
  106. {
  107.     // Add new point to the listbox.
  108.     CString str;
  109.     str.Format(_T("(%i,%i)"), pt.x, pt.y);
  110.     if (nSel == -1)
  111.         nSel = m_ctlList.AddString(str);
  112.     else
  113.         m_ctlList.InsertString(nSel, str);
  114. }
  115.  
  116.  
  117. /////////////////////////////////////////////////////////////////////////////
  118. // CPointArrayView message handlers
  119.  
  120. void CPointArrayView::OnAdd()
  121. {
  122.     if (UpdateData() != TRUE)
  123.         return;
  124.  
  125.     // Add new point to the CList<int,int>
  126.     CPoint pt(m_x, m_y);
  127.     GetDocument()->m_ptArray.Add(pt);
  128.  
  129.     AddPointToListBox(pt);
  130. }
  131.  
  132. void CPointArrayView::OnUpdate()
  133. {
  134.     if (UpdateData() != TRUE)
  135.         return;
  136.  
  137.     int nSel = m_ctlList.GetCurSel();
  138.     if (nSel == LB_ERR)
  139.     {
  140.         AfxMessageBox(IDS_SELECT_POINT_TO_BE_UPDATED);
  141.         return;
  142.     }
  143.  
  144.     // Replace the point in the CArray of CPoints.
  145.     CPoint pt(m_x, m_y);
  146.     GetDocument()->m_ptArray[nSel] = pt;
  147.  
  148.     // Update the old point in the listbox by removing
  149.     // the old entry and adding a new entry.
  150.     m_ctlList.DeleteString(nSel);
  151.     AddPointToListBox(pt, nSel);
  152. }
  153.  
  154. void CPointArrayView::OnRemove()
  155. {
  156.     if (UpdateData() != TRUE)
  157.         return;
  158.  
  159.     int nSel = m_ctlList.GetCurSel();
  160.     if (nSel == LB_ERR)
  161.     {
  162.         AfxMessageBox(IDS_SELECT_POINT_TO_BE_REMOVED);
  163.         return;
  164.     }
  165.  
  166.     // Remove the point from the CArray of CPoints..
  167.     GetDocument()->m_ptArray.RemoveAt(nSel);
  168.  
  169.     // Remove the point from the listbox.
  170.     m_ctlList.DeleteString(nSel);
  171. }
  172.  
  173. void CPointArrayView::OnRemoveAll()
  174. {
  175.     // Remove all of the points from the CArray of CPoints.
  176.     GetDocument()->m_ptArray.RemoveAll();
  177.  
  178.     // Remove all of the points from the listbox.
  179.     m_ctlList.ResetContent();
  180. }
  181.  
  182. void CPointArrayView::OnSelChangeList()
  183. {
  184.     // Update the edit control to reflect the new selection
  185.     // in the listbox.
  186.     CPoint pt = GetDocument()->m_ptArray[m_ctlList.GetCurSel()];
  187.     m_x = pt.x;
  188.     m_y = pt.y;
  189.     UpdateData(FALSE);
  190. }
  191.